<?php

/**
 * @author PhatPham
 * @copyright 2013
 */

class SurveyType extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model(array('SurveyTypemodel', 'Surveymodel')); // Load model
        $this->load->helper(array('form', 'url')); // Load helper url
        $this->load->library('form_validation');
    }

    //View survey types list--------------------------------------------------------------------------
    public function index()
    {
        //get action
        $button = $this->input->post('submit');
        //Create survey type
        if ($button == 'create') {
			//set rules
			$this->form_validation->set_rules('stname', 'Tên loại khảo sát', 'trim|required|xss_clean');
			
            if ($this->form_validation->run() == true) {
                //Get survey type name from view
                $stname = $this->input->post('stname');
                //Create survey type
                $this->SurveyTypemodel->create_survey_type($this->unique_id(), $stname, null);
            }
        }
        //Delete survey type
        else
            if ($button == 'delete') {
                //Get survey type id from view
                $stid = $this->input->post('stid');
                //Delete survey type
                $this->SurveyTypemodel->delete_survey_type($stid, null);
            }

        //Get survey types list from model
        $data['stlist'] = $this->SurveyTypemodel->get_survey_types_list()->result();
        //Set title for view
        $data['title'] = "Quản lý phiếu khảo sát";
        //Load view
        $this->load->view('Templates/Header', $data);
        $this->load->view('SurveyType/List', $data);
        $this->load->view('Templates/Footer');
    }
    
    //edit survey type--------------------------------------------------------------------------------
    public function edit($stid)
    {
        //set rules
        $this->form_validation->set_rules('stname', 'Tên loại khảo sát', 'trim|required|xss_clean');
        
        if ($this->form_validation->run() == true) {
            //get survey type name from view
            $stname = $this->input->post('stname');
            //Edit survey name
            $this->SurveyTypemodel->edit_survey_type($stid, $stname, null);
            //redirect to survey types list
            redirect('surveytype');
        }
        
        //Get survey type from model
        $data['st'] = $this->SurveyTypemodel->get_survey_type($stid);
        //Set title for view
        $data['title'] = "Sửa loại khảo sát";
        //Load view
        $this->load->view('Templates/Header', $data);
        $this->load->view('SurveyType/Edit', $data);
        $this->load->view('Templates/Footer');
    }

    //Check whether this survey type contains any survey--------------------------------------------------
    public function check_delete($stid)
    {
        //get status of survey type from model
        $slist = $this->Surveymodel->get_surveys_list($stid, null);
        echo $slist->num_rows() == 0 ? TRUE : FALSE;
		//false -> can't delete
    }

    //Gererate unique survey type id with format----------------------------------------------------------
    //xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with x is from 0 to 9
    private function unique_id()
    {
        $this->load->helper('string');
        $id = random_string('numeric', 8) . '-' . random_string('numeric', 4) . '-' .
            random_string('numeric', 4) . '-' . random_string('numeric', 4) . '-' .
            random_string('numeric', 12);
        return $id;
    }
}
?>